// models/hive_models.dart
import 'package:hive/hive.dart';

part 'hive_models.g.dart';

@HiveType(typeId: 1)
class BookModel {
  @HiveField(0)
  final String id;

  @HiveField(1)
  String title;

  @HiveField(2)
  String author;

  @HiveField(3)
  String? coverImage;

  @HiveField(4)
  String? description;

  @HiveField(5)
  final DateTime createdAt;

  @HiveField(6)
  DateTime updatedAt;

  @HiveField(7)
  int sortOrder;

  BookModel({
    required this.id,
    required this.title,
    required this.author,
    this.coverImage,
    this.description,
    DateTime? createdAt,
    DateTime? updatedAt,
    this.sortOrder = 0,
  }) : createdAt = createdAt ?? DateTime.now(),
       updatedAt = updatedAt ?? DateTime.now();

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'author': author,
      'coverImage': coverImage,
      'description': description,
      'createdAt': createdAt.millisecondsSinceEpoch,
      'updatedAt': updatedAt.millisecondsSinceEpoch,
      'sortOrder': sortOrder,
    };
  }
}

@HiveType(typeId: 2)
class ChapterModel {
  @HiveField(0)
  final String id;

  @HiveField(1)
  final String bookId;

  @HiveField(2)
  String title;

  @HiveField(3)
  String content;

  @HiveField(4)
  int chapterNumber;

  @HiveField(5)
  final DateTime createdAt;

  @HiveField(6)
  DateTime updatedAt;

  @HiveField(7)
  int wordCount;

  @HiveField(8)
  bool isRead;

  ChapterModel({
    required this.id,
    required this.bookId,
    required this.title,
    required this.content,
    required this.chapterNumber,
    DateTime? createdAt,
    DateTime? updatedAt,
    this.isRead = false,
  }) : createdAt = createdAt ?? DateTime.now(),
       updatedAt = updatedAt ?? DateTime.now(),
       wordCount = content.length;

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'bookId': bookId,
      'title': title,
      'content': content,
      'chapterNumber': chapterNumber,
      'createdAt': createdAt.millisecondsSinceEpoch,
      'updatedAt': updatedAt.millisecondsSinceEpoch,
      'wordCount': wordCount,
      'isRead': isRead,
    };
  }
}
